#! /bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2000,2002 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
#*===========================================================================*/
#*                                                                           */
#* Module Name:  cfgct                                                       */
#*                                                                           */
#* Description:                                                              */
#*      Script to configure the RSCT subsystems at installation              */
#*                                                                           */
#* N.B. This command is intended to be executed by the RMC subsystem only !! */
#*                                                                           */
#*===========================================================================*/
#*   @(#)10   1.9   src/rsct/rmc/mcdaemon/cfgct.sh, mcdaemon, rsct_rori, roris01a 1/29/02 06:08:49 

# Set path to known value
PATH=/usr/sbin/rsct/install/bin:/usr/sbin/rsct/bin:/usr/bin:/bin:/usr/sbin
export PATH

CTROOTDIR=/var/ct		# root directory for all RSCT run-time data
SUBSYS=RSCT_ALL                         # recycle all rsct core subsystems
RECYCLELIST=/var/ct/recycle_list        # list of subsystems to recycle
POSTPROCID='^rsct:'                     # id in post process file
POSTPROCFILE=/var/adm/sw/postprocess    # install post processing file
POSTPROCENTRY='rsct:/usr/sbin/rsct/install/bin/recycle_rsct' # rsct postproc entry
rc=0									# return code

#============================================================================
#
# Test for debug environment variable. If its value is 1, exit immediately
# with a 0 exit value. If it's value is 2, set INUBOSTYPE for testing BOS
# installation. If it's value is 3, set INUCLIENTS for NIM testing
#
#============================================================================

if [[ ${CFGCT_DEBUG:-0} -eq 1 ]]
then
	exit 0
fi

if [[ ${CFGCT_DEBUG:-0} -eq 2 ]]
then
	export INUBOSTYPE=1
fi

if [[ ${CFGCT_DEBUG:-0} -eq 3 ]]
then
	export INUCLIENTS=1
fi

umask 022

#============================================================================
#
# Step 1: make the node id
#
#============================================================================

if ! mknodeid -k
then
	exit 1
fi

#============================================================================
#
# Step 2: check if /var/ct/IW exists. If so, then configuration has already
# been done. Set-up the recycle script to run, then exit 0
#
#============================================================================

if [[ -a $CTROOTDIR/IW ]]
then

    # add SUBSYS to the list if not already there

    if ! grep $SUBSYS $RECYCLELIST > /dev/null 2>&1
    then

        echo $SUBSYS >> $RECYCLELIST
        rc=$?

    fi

    if [[ $rc -eq 0 ]]
    then
        # add recycle script for install postprocessing

        if ! grep $POSTPROCID $POSTPROCFILE > /dev/null 2>&1
        then

            echo "$POSTPROCENTRY" >> $POSTPROCFILE
            rc=$?

        fi
    fi

	exit 0
fi

#============================================================================
#
# Step 3: create required directories and configuration files in /var
#
#============================================================================

# get machine ID

if [[ $(uname -s) = "AIX" ]]
then
	LC_ALL=C uname -x | read r1 r2 MID junk
else
	MID=$(uuidgen)
fi

# create directories, using the machine ID as the cluster ID

for name in                        \
	$CTROOTDIR/$MID                \
	$CTROOTDIR/$MID/cfg            \
	$CTROOTDIR/$MID/lck            \
	$CTROOTDIR/$MID/log            \
	$CTROOTDIR/$MID/registry       \
	$CTROOTDIR/$MID/run            \
	$CTROOTDIR/$MID/soc
do
	if [[ ! -a $name ]]
	then
		if ! mkdir $name
		then
			exit 1
		fi
		chown bin $name
		chgrp bin $name
		chmod 775 $name
	fi
done

# link the cluster name

ln -s $CTROOTDIR/$MID $CTROOTDIR/IW

# make config files

print "$MID IW" > $CTROOTDIR/cfg/clusters
print "$MID" > $CTROOTDIR/cfg/default_cluster
print "$MID" > $CTROOTDIR/cfg/current_cluster

print "1 $MID localhost $MID IW 1" > $CTROOTDIR/IW/cfg/nodedef.cfg

# create the "first start" file

touch $CTROOTDIR/first_start

#============================================================================
#
# Step 4: add the RMC subsystem and start it, unless INUCLIENTS is set
# or INUBOSTYPE is set
#
#============================================================================

if rmcctrl -a
then
	if [[ -z "$INUCLIENTS" && -z "$INUBOSTYPE" ]]
	then
		if ! rmcctrl -s
		then
			exit 1
		fi
	fi
else
	exit 1
fi

exit 0
